package com.lu.action;

import java.io.PrintWriter;
import java.text.DateFormat;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.lu.utils.JacksonUtils;
import com.opensymphony.xwork2.ActionSupport;


/**
 * Action共同
 *
 */
public class BaseAction extends ActionSupport implements ServletRequestAware, ServletResponseAware {

    private static final long serialVersionUID = 7834736236105975470L;
    protected static final String JSON = "JSON";

    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected HttpSession session;
    private Vector<String> errMessage;
    protected int start;
    protected int limit;
    protected int pageSize = 30;
    protected int Total;
    protected int page = 1;

    public void setServletRequest(HttpServletRequest req) {
        this.request = req;
        this.session = request.getSession();
    }

    public void setServletResponse(HttpServletResponse res) {
        this.response = res;
    }

    public void writeError() {
        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter writer = response.getWriter();
            writer.write("{success:false,msg:'");
            for (String msg : errMessage) {
                writer.write(msg);
                writer.write("<br/>");
            }
            errMessage.clear();
            writer.write("'}");
            writer.flush();
            writer.close();
        } catch (java.io.IOException exc) {
            exc.printStackTrace();
        }
    }

    public void writeJson(Object obj) {
        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter writer = response.getWriter();
            writer.write(JacksonUtils.writeValueAsString(obj));
            writer.flush();
            writer.close();
        } catch (java.io.IOException exc) {
            exc.printStackTrace();
        }
    }

    public void writeJson(String s) {
        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter writer = response.getWriter();
            writer.write(s);
            writer.flush();
            writer.close();
        } catch (java.io.IOException exc) {
            exc.printStackTrace();
        }
    }

    public void writeJsonWithDateFormat(Object obj, DateFormat formatter) {
        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter writer = response.getWriter();
            writer.write(JacksonUtils.writeValueAsStringWithDateFormat(obj, formatter));
            writer.flush();
            writer.close();
        } catch (java.io.IOException exc) {
            exc.printStackTrace();
        }
    }

    public void writeJson(Object obj, boolean isSuccess) {
        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter writer = response.getWriter();
            StringBuffer jsonData = new StringBuffer("");
            if (isSuccess) {
                jsonData.append("{success:true,data:");
            } else {
                jsonData.append("{success:false,data:");
            }
            jsonData.append(JacksonUtils.writeValueAsString(obj));
            jsonData.append("}");
            write(jsonData.toString());
        } catch (java.io.IOException exc) {
            exc.printStackTrace();
        }
    }

    public void write(String outStr) {
        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter writer = response.getWriter();
            writer.write(outStr);
            writer.flush();
            writer.close();
        } catch (java.io.IOException exc) {
            exc.printStackTrace();
        }
    }

    public void writeXml(String outStr) {
        try {
            response.setContentType("text/xml;charset=utf-8");
            PrintWriter writer = response.getWriter();
            writer.write(outStr);
            writer.flush();
            writer.close();
        } catch (java.io.IOException exc) {
            exc.printStackTrace();
        }
    }

    protected void addError(String msgId, Object... params) {
        if (errMessage == null) {
            errMessage = new Vector<String>();
        }
        errMessage.add(String.format(getText(msgId), params));
    }

    protected void addErrorWithMsg(String msg, Object... params) {
        if (errMessage == null) {
            errMessage = new Vector<String>();
        }
        errMessage.add(String.format(msg, params));
    }

    public boolean isInvalid() {
        if (errMessage != null && errMessage.size() > 0) {
            writeError();
            return true;
        }
        return false;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotal() {
        return Total;
    }

    public void setTotal(int total) {
        Total = total;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }
}
